package com.xjtu.platform.user.assister.consultArrange;
import com.xjtu.platform.common.*;
import com.xjtu.platform.common.model.FirstVisit;
import com.xjtu.platform.common.model.User;
import com.xjtu.platform.user.student.firstVisit.FirstVisitService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/assister/consultArrange")
public class ConsultArrangeAPI {
    @Resource
    CommonService commonService;

    @Resource
    ConsultArrangeService consultArrangeService;

    @Resource
    FirstVisitService firstVisitService;

    @RequestMapping(value = "/checkAllApply",method = RequestMethod.POST)
    Result checkAllApply(@RequestHeader("Token") String Token)
    {
        if(Token == null){
            System.out.println("查询当前用户时无令牌");
        }
        try {
            String user_id = TokenUtils.getUserInfo(Token,commonService).getUserId();
            FirstVisit dto = new FirstVisit();
            dto.setStudent_id("%");
            dto.setStatus("waitConsult");
            dto.setType("needConsult");
            Map<String,Object> page = firstVisitService.getInfo(dto);
            return Result.success(page);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.fail(400,"查询当前用户失败！");
    }

    @RequestMapping(value = "/getAllConsultOfVisit",method = RequestMethod.POST)
    Result getAllConsultOfVisit(@RequestBody FirstVisit dto, @RequestHeader("Token") String Token)
    {
        if(Token == null){
            System.out.println("查询当前用户时无令牌");
        }
        try {
            System.out.println(dto.getVisit_id());
            List<FillBlankDto> consults = consultArrangeService.getAllConsultOfVisit(dto);
            return Result.success(consults);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.fail(400,"查询当前用户失败！");
    }

    @RequestMapping(value = "/getAllTeachers",method = RequestMethod.POST)
    Result getAllTeachers(@RequestHeader("Token") String Token)
    {
        if(Token == null){
            System.out.println("查询当前用户时无令牌");
        }
        try {
            List<User> consults = consultArrangeService.getAllTeachers();
            return Result.success(consults);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.fail(400,"查询当前用户失败！");
    }

    @RequestMapping(value = "/getOcupied",method = RequestMethod.POST)
    Result getOcupied(@RequestHeader("Token") String Token)
    {
        if(Token == null){
            System.out.println("查询当前用户时无令牌");
        }
        try {
            List<User> consults = consultArrangeService.getAllTeachers();
            return Result.success(consults);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.fail(400,"查询当前用户失败！");
    }

    @PostMapping("/getFreeTime")
    public Result searchFreeTime(@RequestBody FreeTimeDto freeTimeDto){
        try {
            Map<String,Object> page = consultArrangeService.getFreeTime(freeTimeDto);
            return Result.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(Result.ERR_CODE_SYS,"系统维护中.......");
        }
    }


    @PostMapping("/finishArrange")
    public Result finishArrange(@RequestBody FirstVisit dto){
        int code = consultArrangeService.finishArrange(dto);
        if(code==Constants.CONFIRM_ALL_ARRANGE_SUCCESS)
        {
            return Result.success();
        }
        else if (code==Constants.NOT_COMPLETE_ARRANGE)
        {
            return Result.fail(code,"尚有未完成安排的咨询");
        }else {
            return Result.fail(Result.ERR_CODE_SYS,"系统维护中.......");
        }
    }

    @PostMapping("/fillBlanks")
    public Result fillBlanks(@RequestBody FillBlankDto dto){
        try {
            System.out.println("qqqqqqqqqqqq");
            System.out.println(dto.getCount_num());
            System.out.println(dto.getPlace_name());
            consultArrangeService.fillBlanks(dto);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(Result.ERR_CODE_SYS,"系统维护中.......");
        }
    }

}
